// Copyright DustedPixels.com 2008. All rights reserved.

package com.dustedpixels.jasmin.unit.z80.v2;

/**
 * Registers: BC, DE, HL, IX, BC', DE', HL', IY.
 * 
 * @author micapolos@gmail.com (Michal Pociecha-Los)
 */
public final class Regs {
  public boolean CLK;
  public boolean WRITE_HI;
  public boolean WRITE_LO;
  public int REG_IN;
  public int REG_OUT;
  public int REG_OUT16;
  public int IN_HI;
  public int IN_LO;
  public int OUT_HI;
  public int OUT_LO;
  public int OUT16;
  
  private int[] hiArray = new int[8];
  private int[] loArray = new int[8];
  
  public void clkHiTrigger() {
    if (WRITE_HI) {
      hiArray[REG_IN & 0x07] = IN_HI & 0xFF;
    }
    
    if (WRITE_LO) {
      loArray[REG_IN & 0x07] = IN_LO & 0xFF;
    }
  }
  
  public void update() {
    int regOut = REG_OUT & 0x07;
    int regOut16 = REG_OUT16 & 0x07;
    
    OUT_HI = hiArray[regOut];
    OUT_LO = loArray[regOut];

    OUT16 = (hiArray[regOut16] << 8) | loArray[regOut16];
  }
}
